﻿// ******************************************************************
// 项目名称 :WSPay
// 项目描述 :
// 文件名称 : UrlExtenstions.cs
// 版  本 : v1.0.0.0
// 说  明 :  
// 作  者 : mengzhong
// 邮  箱 :coodream@live.com
// 命名空间 : WSPay.Core
// 创建时间 : 2017.04.21.11:18
// 更新时间 : 2017.04.21.11:18
// ******************************************************************
//  Copyright meng zhong  2017. All rights reserved.
// ******************************************************************

using System;
using System.Web.Mvc;

namespace WSPay.Common
{
    public static class UrlExtenstions
    {
        /// <summary>
        /// Returns an absolute url for an action
        /// </summary>
        /// <param name="url"></param>
        /// <param name="controller">Controller Name</param>
        /// <param name="action">Action Name</param>
        /// <param name="routeValues"></param>
        /// <returns></returns>
        public static string AbsoluteAction(this UrlHelper url, string action, string controller, object routeValues)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
            string absoluteAction = "{0}://{1}{2}".FormatWith(requestUrl.Scheme, requestUrl.Authority, url.Action(action, controller, routeValues));
            return absoluteAction;
        }

        /// <summary>
        /// Returns an absolute url for an action
        /// </summary>
        /// <param name="url"></param>
        /// <param name="controller"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static string AbsoluteAction(this UrlHelper url, string action, string controller)
        {
            return url.AbsoluteAction(action, controller, new object { });
        }

        /// <summary>
        /// Returns an absolute url for an action
        /// </summary>
        /// <param name="url"></param>
        /// <param name="action"></param>
        /// <param name="routeValues"></param>
        /// <returns></returns>
        public static string AbsoluteAction(this UrlHelper url, string action, object routeValues)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
            string absoluteAction = "{0}://{1}{2}".FormatWith(requestUrl.Scheme, requestUrl.Authority, url.Action(action, routeValues));
            return absoluteAction;
        }

        /// <summary>
        /// Returns an absolute url for an action
        /// </summary>
        /// <param name="url"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static string AbsoluteAction(this UrlHelper url, string action)
        {
            return url.AbsoluteAction(action, new object { });
        }

        public static string Content(this UrlHelper url, string contentPath, bool absolute)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
            if (absolute)
            {
                string absoluteAction = "{0}://{1}{2}".FormatWith(requestUrl.Scheme, requestUrl.Authority, url.Content(contentPath));
                return absoluteAction;
            }
            return url.Content(contentPath);

        }

        public static string Absolute(this UrlHelper url, string path, bool https = false)
        {
            return url.Content(path, true, https);
        }

        public static string Content(this UrlHelper url, string contentPath, bool absolute, bool isHttps = false)
        {
            Uri requestUrl = url.RequestContext.HttpContext.Request.Url;
            var https = isHttps ? "https" : "http";
            if (absolute)
            {
                string absoluteAction = "{0}://{1}{2}".FormatWith(https, requestUrl.Authority, url.Content(contentPath));
                return absoluteAction;
            }
            return url.Content(contentPath);

        }
     
    }
}